package com.example.educationcommunity.controller;

import com.example.educationcommunity.dto.QuestionDTO;
import com.example.educationcommunity.mapper.QuestionMapper;
import com.example.educationcommunity.mapper.UserMapper;
import com.example.educationcommunity.model.Question;
import com.example.educationcommunity.model.QuestionExample;
import com.example.educationcommunity.model.User;
import com.example.educationcommunity.service.QuestionService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

@Controller
public class SetTopController {
    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private UserMapper userMapper;

    @GetMapping("/settop/{id}")
    public String setTop(@PathVariable(name = "id") String id,
                         Model model){
        Question question  = questionMapper.selectByPrimaryKey(Integer.parseInt(id));

        if(question.getIsTop() == true){
            question.setIsTop(false);
        }else{
            question.setIsTop(true);
        }

        QuestionExample questionExample = new QuestionExample();
        questionExample.createCriteria()
                .andIdEqualTo(Integer.parseInt(id));
        questionMapper.updateByExampleSelective(question,questionExample);

        return "redirect:/";
    }
}
